package com.jsu.service;

import java.io.IOException;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jsu.bean.CurrentMax;
import com.jsu.bean.FoundMessage;
import com.jsu.bean.LostMessage;
import com.jsu.dao.FoundMessageMapper;
import com.jsu.dao.LostMessageMapper;
import com.jsu.face.Face_Operate;
import com.jsu.face.FindFaceSetOutid;
import com.jsu.face.LostFaceSetOutid;
@Component
public class GetFoundFaceToken implements Runnable{
	
	@Autowired
	CurrentMax currentMax;
	@Autowired
	FoundMessageMapper foundMessageMapper;
	@Autowired
	Face_Operate face_Operate;
	@Autowired
	FoundMessage updateFoundMessage;
	
	String findFaceSetID=FindFaceSetOutid.getInstance().getFindFaceSetID();
	String photoBasic;
	
	public void setTar(CurrentMax currentMax ,String photoBasic){
		this.currentMax.setCurrentId(currentMax.getCurrentId());
		this.currentMax.setMaxId(currentMax.getMaxId());
		this.photoBasic=photoBasic;
	}

	@Override
	public void run() {
		int currentId = currentMax.getCurrentId();
		int maxId = currentMax.getMaxId();
		for(int i=currentId+1;i<=maxId;i++){
			FoundMessage foundMessage = foundMessageMapper.selectByPrimaryKey(i);
			if(foundMessage!=null){
				String photopath=foundMessage.getPhotopath();
				if(photopath!=null){
						byte[] photoFile;
						try {
							photoFile = face_Operate.toByteArray(photoBasic+photopath);
							String facetoken = face_Operate.toFaceToken(photoFile);
							if(!facetoken.equals("failed")){
								face_Operate.add_faceset(facetoken, findFaceSetID);
							}
							updateFoundMessage.setFoundmessageId(foundMessage.getFoundmessageId());
							updateFoundMessage.setFacetoken(facetoken);
							foundMessageMapper.updateByPrimaryKeySelective(updateFoundMessage);
						} catch (IOException e) {
							e.printStackTrace();
						}
						
				}
				
			}
		}
		
	}
	

}
